package com.eddie.singleton.type3;

/**
 * @author Eddie
 * @date 2022/4/29 9:18
 */
public class SingletonTest03 implements Runnable {
    public static void main(String[] args) {
        for (int i = 0; i < 200; i++) {
            SingletonTest03 singletonTest03 = new SingletonTest03();
            new Thread(singletonTest03).start();
        }
    }

    @Override
    public void run() {
        Singleton instance = Singleton.getInstance();
        System.out.println(instance.hashCode());
    }
}

/**
 * 懒汉式（线程不安全）
 */
class Singleton {

    private static Singleton instance;

    private Singleton() {

    }


    /**
     * 提供一个静态公有方法，当时用到该方法时，才去创建instance
     * 即懒汉式
     */
    public static Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }
}
